package com.orange.quickback.system.handler;

import com.baomidou.kaptcha.exception.KaptchaException;
import com.baomidou.kaptcha.exception.KaptchaIncorrectException;
import com.baomidou.kaptcha.exception.KaptchaNotFoundException;
import com.baomidou.kaptcha.exception.KaptchaTimeoutException;
import com.orange.quickback.system.api.common.result.QBootError;
import com.orange.quickback.system.api.common.result.QBootException;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

@RestControllerAdvice
public class GlobalExceptionHandler{


    /**
     * 定义要捕获的异常 可以多个 @ExceptionHandler({})
     *
     * @param request  request
     * @param e        exception
     * @param response response
     * @return 响应结果
     */
    @ExceptionHandler(value = QBootException.class)
    public QBootError customExceptionHandler(HttpServletRequest request, final Exception e, HttpServletResponse response) {
        response.setStatus(HttpStatus.BAD_REQUEST.value());
        QBootException exception = (QBootException) e;
        return new QBootError(exception.getCode(), exception.getMessage());
    }

    /**
     * 捕获  RuntimeException 异常
     * TODO  如果你觉得在一个 exceptionHandler 通过  if (e instanceof xxxException) 太麻烦
     * TODO  那么你还可以自己写多个不同的 exceptionHandler 处理不同异常
     *
     * @param request  request
     * @param e        exception
     * @param response response
     * @return 响应结果
     */
    @ExceptionHandler(value =RuntimeException.class)
    public QBootError runtimeExceptionHandler(HttpServletRequest request, final Exception e, HttpServletResponse response) {
        response.setStatus(HttpStatus.BAD_REQUEST.value());
        RuntimeException exception = (RuntimeException) e;
        return new QBootError(400, exception.getMessage());
    }

    /**
     * 通用的接口映射异常处理方l
     */

    @ExceptionHandler(value =AccessDeniedException.class)
    public ResponseEntity<Map<String,String>> globalException(HttpServletRequest request, Exception exception){

        Map<String,String> map = new HashMap<>();
        map.put("code","500");
        map.put("result","error");
        map.put("message","网络高峰期，等待红绿灯！");
        return new ResponseEntity<Map<String,String>>(map, HttpStatus.OK);
    }

    @ExceptionHandler(value = KaptchaException.class)
    public QBootError kaptchaExceptionHandler(KaptchaException kaptchaException) {
        if (kaptchaException instanceof KaptchaIncorrectException) {
            return new QBootError(400, "验证码不正确");
        } else if (kaptchaException instanceof KaptchaNotFoundException) {
            return new QBootError(400, "验证码未找到");
        } else if (kaptchaException instanceof KaptchaTimeoutException) {
            return new QBootError(400, "验证码过期");
        } else {
            return new QBootError(400, "验证码渲染失败");
        }

    }
}
